<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');
//require_once(dirname(__FILE__) . '/current.php');

need_manager();
need_auth('team');

$id = abs(intval($_GET['id']));
$news  = Table::Fetch('news', $id);

if ( is_get() && empty($news) ) {
	$news = array();
	$news['id'] = 0;
	$news['begin_time'] = strtotime('+0 days');
	$news['end_time'] = strtotime('+5 days');
}
else if ( is_post() ) {
	$news = $_POST;
	$insert = array(
		'image',
	'type', 
		'title', 
		'begin_time', 
		'detail',
				'tuijian',
		);
	
	$news['begin_time'] = strtotime($news['begin_time']);
	$news['type'] = abs(intval($news['type']));
		$news['tuijian'] = abs(intval($news['tuijian']));
	$insert = array_unique($insert);
	$table = new Table('news', $news);
	$table->SetStrip('detail');
		$table->image = upload_image('upload_image', null, 'team', true);







	if ( $news['id'] && $news['id'] == $id ) {
		$table->SetPk('id', $id);
		$table->update($insert);
		Session::Set('notice', '添加新闻成功！');
		redirect( WEB_ROOT . "/manage/news/index.php");
	} 
	else if ( $news['id'] ) {
		Session::Set('error', '非法编辑');
		redirect( WEB_ROOT . "/manage/news/index.php");
	}

	if ( $table->insert($insert) ) {
		Session::Set('notice', '添加新闻成功');
		redirect( WEB_ROOT . "/manage/news/index.php");
	}
	else {
		Session::Set('error', '编辑新闻失败');
		redirect(null);
	}
}

$groups = DB::LimitQuery('category', array(
			'condition' => array( 'zone' => 'news','fid' => '0', ),
			));
$groups = Utility::OptionArray($groups, 'id', 'name');

$selector = $news['id'] ? 'edit' : 'create';
include template('manage_news_edit');
function current_managenews($selector='edit', $id=0) {
	$selector = $selector ? $selector : 'edit';
	$a = array(
		"/manage/news/edit.php?id={$id}" => '基本信息',
	);
	$l = "/manage/news/{$selector}.php?id={$id}";
	return current_link($l, $a);
}

